<?php

include_once('./common.php');
include_once(S_ROOT.'./source/function_admin.php');
include_once(S_ROOT.'./source/function_ui.php');
if (!defined('IN_ADMIN')) {
    exit('Access Denied');
}

$url = "http://test.ffrj.net/adminApi.php";
$admin_token = '1ee4735dfd4ab146c55e684d63e0b82a';
$admin_uid   = 5687598;

if ($_GET['action'] == "changeExpLevel"){
    $uid   = intval($_GET ['uid']);
    $exp   = intval($_GET ['exp']);
    if ($uid < 1){
        return FALSE;
    }

    $param = array(
            'do'    => 'changeExpLevel',
            'format'=> 'json',
            'uid'   =>  $uid,
            'exp'   =>	$exp,
            'app'   =>  'checkin'
    );
    $content = changeExpLevel($param, $url, $admin_token, $admin_uid);
}elseif($_GET['action'] == 'getUserInfo'){
    $uid   = intval($_GET ['uid']);
    if ($uid < 1){
        return FALSE;
    }

    $content = getUserInfo($uid, $url, $admin_token, $admin_uid);
    echo $content;
    exit();
}elseif($_GET['action'] == 'getForbInfo'){
    $uid   = intval($_GET ['uid']);
    if ($uid < 1){
        return FALSE;
    }

    $content = getForbInfo($uid, $url, $admin_token, $admin_uid);
    echo $content;
    exit();
}elseif($_GET['action'] == 'getGagUserInfo'){
    $uid   = intval($_GET ['uid']);
    if ($uid < 1){
        return FALSE;
    }

    $content = getGagUserInfo($uid, $url, $admin_token, $admin_uid);
    echo $content;
    exit();
}elseif($_GET['action'] == 'getDiaryInfo'){

    $uid   = intval($_GET ['uid']);
    $bodyId = intval($_GET ['bodyId']);
    if ($uid < 1 || $bodyId < 1){
        return FALSE;
    }

    $content = getDiaryInfo($uid, $bodyId, $url, $admin_token, $admin_uid);
    echo $content;
    exit();
}elseif($_GET['action'] == 'getTopicInfo'){
    $tid   = intval($_GET ['tid']);
    if ($tid < 1){
        return FALSE;
    }

    $content = getTopicInfo($tid, $url, $admin_token, $admin_uid);
    echo $content;
    exit();
}elseif($_GET['action'] == 'getChatGroupInfo'){
    $uid   = $admin_uid;
    $gid   = intval($_GET ['gid']);
    if ($uid < 1 || $gid < 1){
        return FALSE;
    }

    $content = getChatGroupInfo($uid, $gid, $url, $admin_token, $admin_uid);
    echo $content;
    exit();
}elseif ($_GET['action'] == 'getAllTopics'){
    $uid     = $admin_uid;
    $start   = 0;
    $length  = 10;
    
    isset($_GET ['start'])    &&    $start   = intval($_GET ['start']);
    isset($_GET ['length'])   &&    $length  = intval($_GET ['length']);
    if ($uid < 1 || $length < 1){
        return FALSE;
    }
    
    $content = getAllTopics($uid, $start, $length, $url, $admin_token, $admin_uid);
    echo $content;
    exit();
}elseif ($_GET['action'] == 'postTopicComment'){
    $uid       = intval($_GET ['uid']);
    $tid       = intval($_GET ['tid']);
    $author_uid= intval($_GET ['author_uid']);
    $content   = strval($_GET ['content']);
    if ($uid < 1 || $tid < 1 || $author_uid < 1 || strlen($content) < 1){
        return FALSE;
    }

    $content = postTopicComment($uid, $tid, $author_uid, $content, $url, $admin_token, $admin_uid);
}elseif($_GET['action'] == 'updateBoardActiveUsers'){
    $uids = strval(trim($_GET['uids']));
    if (empty($uids)) {
        return FALSE;
    }
    
    $content = updateBoardActiveUsers($uids, $url, $admin_token, $admin_uid);
}elseif ($_GET['action'] == 'postDiaryComment') {
    $uid       =    intval($_GET['uid']);
    $cUid      =    intval($_GET['cUid']);
    $bodyId    =    intval($_GET['bodyId']);
    $content   =    strval($_GET ['content']);
    if ($uid < 1 || $cUid < 1 || $bodyId < 1 || strlen($content) < 1){
        return FALSE;
    }
    
    //$content = postDiaryComment($uid, $bodyId, $cUid, $content, $url, $admin_token, $admin_uid);
    $content = postTopicDiaryComment($uid, $bodyId, $cUid, $content, $url, $admin_token, $admin_uid);
}elseif ($_GET['action'] == 'getHotDiary'){
    $uid     = $admin_uid;
    $start   = 0;
    $length  = 100;
    
    isset($_GET ['start'])    &&    $start   = intval($_GET ['start']);
    isset($_GET ['length'])   &&    $length  = intval($_GET ['length']);
    if ($uid < 1 || $length < 1){
        return FALSE;
    }
    
    $content = getHotDiary($uid, $start, $length, $url, $admin_token, $admin_uid);
    echo $content;
    exit();
}elseif ($_GET['action'] == 'getAbilityUids'){
    $content = getAbilityUids($url, $admin_token, $admin_uid);
    echo $content;
    exit();
}elseif ($_GET['action'] == 'sendMessage'){
    $uid           = intval($_GET['uid']);
    $mUid          = intval($_GET['mUid']);
    $content       = getstr($_GET['content'], 512);
    $data_image    = strval($_GET['data_image']);
    $data_title    = strval($_GET['data_title']);
    $data_content  = strval($_GET['data_content']);
    $data_action   = strval($_GET['data_action']);

    if ($uid < 1 || $mUid < 1 || strlen($content) < 1){
        return FALSE;
    }
    
    $content = sendMessage($uid, $mUid, $content, $data_action, $data_image, $data_title, $data_content, $url, $admin_token, $admin_uid);
}elseif ($_GET['action'] == 'writeDiary'){
    $uid           = intval($_GET['uid']);
    $title         = getstr($_GET['title'], 100);
    $content       = getstr($_GET['content'], 1024);
    $attachment    = $_GET['attachment'];
    
    if ($uid < 1 || strlen($content) < 1){
        return FALSE;
    }
    
    $content = writeDiary($uid, $title, $content, $attachment, $url, $admin_token, $admin_uid);
}else{
    $content = removeAction($_GET['action'], $url);
}

// 发布日记
function writeDiary ($uid, $title, $content, $attachment, $url, $admin_token, $admin_uid) {
    $param = array(
        'do'          =>    'writeDiary',
        'format'      =>    'json',
        'uid'         =>    $uid,
        'title'       =>    urlencode($title),
        'content'     =>    urlencode($content),
        'attachment'  =>    json_encode($attachment),
        'admin_post'  =>    1
    );

    // 请求服务器接口
    $content = get_api_content($url, $param, $admin_token, $admin_uid);

    $content = json_decode($content, TRUE);
    $result  = json_encode($content['resultData']);
    return $result;
}

function getAbilityUids($url, $admin_token, $admin_uid) {
    $param = array(
            'do'          =>    'ability',
            'action'      =>    'getAbilityUids',
            'format'      =>    'json'
    );
    // 请求服务器接口
    $content = get_api_content($url, $param, $admin_token, $admin_uid);
    $content = json_decode($content, TRUE);
    $result  = json_encode($content['resultData']);
    return $result;
}

// 发私信
function sendMessage($uid, $mUid, $content, $data_action, $data_image, $data_title, $data_content, $url, $admin_token, $admin_uid){
    $param = array(
            'do'           => 'adminSendMessage',
            'format'       => 'json',
            'uid'          => $uid,
            'mUid'         => $mUid,
            'data_action'  => $data_action,
            'data_image'   => $data_image,
            'data_title'   => $data_title,
            'data_content' => $data_content,
            'content'      => $content
    );

    $content = get_api_content($url, $param, $admin_token, $admin_uid);
    $content = json_decode($content);
    return $content;
}

// 获取热门日记
function getHotDiary($uid, $start, $length, $url, $admin_token, $admin_uid) {
    $param = array(
            'uid'         =>    $uid,
            'do'          =>    'getHotDiaryReady',
            'format'      =>    'json',
            'category'    =>    0,
            'start'       =>    intval($start),
            'length'      =>    intval($length)
    );
    // 请求服务器接口
    $content = get_api_content($url, $param, $admin_token, $admin_uid);
    $content = json_decode($content, TRUE);
    $result = json_encode($content['resultData']);
    return $result;
}

// 评论日记
function postDiaryComment($uid, $bodyId, $cUid, $content, $url, $admin_token, $admin_uid){
    $param = array(
            'uid'        =>    intval($uid),
            'cUid'       =>    intval($cUid),
            'bodyId'     =>    intval($bodyId),
            'content'    =>    urlencode(getstr($content, 15000)),
            'type'       =>    1,
            'do'         =>    'postDiaryComment',
            'format'     =>    'json'
    );

    // 请求服务器接口
    $content = get_api_content($url, $param, $admin_token, $admin_uid);
    $content = json_decode($content);
    return $content;
}

function postTopicDiaryComment($uid, $bodyId, $cUid, $content, $url, $admin_token, $admin_uid){
    $param = array(
        'uid'        =>    intval($uid),
        'author_uid' =>    intval($cUid),
        'tid'        =>    intval($bodyId),
        'content'    =>    urlencode(getstr($content, 15000)),
        'type'       =>    1,
        'do'         =>    'comment',
        'action'     =>    'commentDiary',
        'format'     =>    'json'
    );

    // 请求服务器接口
    $content = get_api_content($url, $param, $admin_token, $admin_uid);
    $content = json_decode($content);
    return $content;
}

//评论话题
function postTopicComment($uid, $tid, $author_uid, $content, $url, $admin_token, $admin_uid){
    $param = array(
            'uid'        =>    intval($uid),
            'tid'        =>    intval($tid),
            'author_uid' =>    intval($author_uid),
            'content'    =>    urlencode(getstr($content, 15000)),
            'nocheck'    =>    1,
            'do'         =>    'postTopicComment',
            'format'     =>    'json'
    );
    
    // 请求服务器接口
    $content = get_api_content($url, $param, $admin_token, $admin_uid);
    $content = json_decode($content);
    return $content; 
}

// 按时间排序，获取全部话题
function getAllTopics($uid, $start, $length, $url, $admin_token, $admin_uid){
    $param = array(
            'uid'         =>    $uid,
            'do'          =>    'getAllTopics',
            'format'      =>    'json',
            'start'       =>    intval($start),
            'length'      =>    intval($length)
    );
    // 请求服务器接口
    $content = get_api_content($url, $param, $admin_token, $admin_uid);
    $content = json_decode($content, TRUE);
    $result = json_encode($content['resultData']);
    return $result;
}

// 获取用户信息
function getUserInfo($uid, $url, $admin_token, $admin_uid){
    $param = array(
            'uid'         =>    $uid,
            'do'          =>    'getUserInfo',
            'format'      =>    'json'
    );
    // 请求服务器接口
    $content = get_api_content($url, $param, $admin_token, $admin_uid);
    $content = json_decode($content, TRUE);
    $result = json_encode($content['resultData']);
    return $result;
}

// 获取设备封禁情况
function getForbInfo($uid, $url, $admin_token, $admin_uid){
    $param = array(
            'uid'         =>    $uid,
            'do'          =>    'getForbInfo',
            'format'      =>    'json'
    );
    // 请求服务器接口
    $content = get_api_content($url, $param, $admin_token, $admin_uid);
    $content = json_decode($content,true);
    $result = json_encode($content['resultData']);
    return $result;
}

// 获取禁言情况
function getGagUserInfo($uid, $url, $admin_token, $admin_uid){
    $param = array(
            'uid'         =>    $uid,
            'do'          =>    'getGagUserInfo',
            'format'      =>    'json'
    );
    // 请求服务器接口
    $content = get_api_content($url, $param, $admin_token, $admin_uid);
    $content = json_decode($content,true);
    $result = json_encode($content['resultData']);
    return $result;
}

// 获取日记信息
function getDiaryInfo($uid, $bodyId, $url, $admin_token, $admin_uid){
    $param = array(
            'do'          =>    'getDiaryInfoByBodyId',
            'format'      =>    'json',
            'uid'         =>    $uid,
            'bodyId'      =>    $bodyId,
            'no_host_uid' =>    1
    );
    // 请求服务器接口
    $content = get_api_content($url, $param, $admin_token, $admin_uid);
    $content = json_decode($content, TRUE);
    $result = json_encode($content['resultData']);
    return $result;
}

// 获取话题信息
function getTopicInfo($tid, $url, $admin_token, $admin_uid){
    $param = array(
            'format'      =>    'json',
            'do'          =>    'group',
            'action'      =>    'getTopicInfo',
            'tid'         =>    $tid,
    );
    // 请求服务器接口
    $content = get_api_content($url, $param, $admin_token, $admin_uid);
    $content = json_decode($content, TRUE);
    $result = json_encode($content['resultData']);
    return $result;
}

// 获取群组信息
function getChatGroupInfo($uid, $gid, $url, $admin_token, $admin_uid){
    $param = array(
            'format'      =>    'json',
            'do'          =>    'groupChat',
            'action'      =>    'getChatGroupInfo',
            'uid'         =>    $uid,
            'gid'         =>    $gid,
    );
    // 请求服务器接口
    $content = get_api_content($url, $param, $admin_token, $admin_uid);
    $content = json_decode($content, TRUE);
    $result = json_encode($content['resultData']);
    return $result;
}

// 签到修改经验值
function changeExpLevel($param, $url, $admin_token, $admin_uid){
    $content = get_api_content($url, $param, $admin_token, $admin_uid);
    $content = json_decode($content);
    return $content;
}

// 更新redis榜单用户
function updateBoardActiveUsers($uids, $url, $admin_token, $admin_uid){
    $param = array(
            'format'      =>    'json',
            'do'          =>    'updateBoardActiveUsers',
            'uids'        =>    $uids
    );
    // 请求服务器接口
    $content = get_api_content($url, $param, $admin_token, $admin_uid);
    $content = json_decode($content);
    return $content;
}

// 管理员删除操作
function removeAction($action, $url){
    if ($action == 'removeDiaryComment'){
        $id  = intval($_GET['commentId']);
        $uid = intval($_GET ['uid']);
        if ($id < 1 || $uid < 1){
            return FALSE;
        }
    
        $param = array(
                'uid'     =>    $uid,
                'rUid'    =>    $uid,
                'id'      =>    $id,
                'do'      =>    'removeComment',
                'format'  =>    'json',
        );
    
    }elseif ($action == 'removeDiary'){
        $bodyId  = intval($_GET['bodyId']);
        $uid     = intval($_GET ['uid']);
        if ($bodyId < 1 || $uid < 1){
            return FALSE;
        }
    
        $param = array(
                'uid'     =>    $uid,
                'bodyId'  =>    $bodyId,
                'do'      =>    'removeDiary',
                'format'  =>    'json',
        );
    
    }elseif ($action == 'removeTopic'){
        $tid  = intval($_GET['tid']);
        $uid  = intval($_GET ['uid']);
        if ($tid < 1 || $uid < 1){
            return FALSE;
        }
    
        $param = array(
                'tid'         =>    $tid,
                'author_uid'  =>    $uid,
                'do'          =>    'removeTopic',
                'format'      =>    'json'
        );
    
    }elseif ($action == 'removeTopicComment'){
        $uid      = intval($_GET ['author_uid']);
        $tid      = intval($_GET ['tid']);
        $position = intval($_GET['position']);
        if ($tid < 1 || $uid < 1 || $position < 1){
            return FALSE;
        }
    
        $param = array(
                'tid'         =>    $tid,
                'author_uid'  =>    $uid,
                'position'    =>    $position,
                'do'          =>    'removeTopicComment',
                'format'      =>    'json'
        );
    }elseif ($action == 'revertTopic') {
        $tid  = intval($_GET['tid']);
        $uid  = intval($_GET ['uid']);
        if ($tid < 1 || $uid < 1 ){
            return FALSE;
        }
        
        $param = array(
                'tid'         =>    $tid,
                'author_uid'  =>    $uid,
                'do'          =>    'revertTopic',
                'format'      =>    'json'
        );
        
    }elseif ($action == 'restoreDiary') {
        $uid      = intval($_GET ['uid']);
        $bodyId   = intval($_GET ['bodyId']);
        if ($uid < 1 || $bodyId < 1){
            return FALSE;
        }
        
        $param = array(
                'uid'         =>    $uid,
                'bodyId'      =>    $bodyId,
                'do'          =>    'restoreDiary',
                'format'      =>    'json'
        );
    }
    
    // 管理系统认证
    if (!empty($_GET['sid'])){
        $sid = $_GET['sid'];
        session_id($sid);
        session_start();
    }
    
    if (!isset($_SESSION['user'])){
        return FALSE;
    }
    
    $token     = $_SESSION['user']['token'];
    $adminUid  = $_SESSION['user']['adminUid'];
    
    if (empty($token) || empty($adminUid)){
        return FALSE;
    }

    // 请求服务器接口
    $content = get_api_content($url, $param, $token , $adminUid );
    $content = json_decode($content);
    return $content;  
}

if ($content->resultData) {
    echo 1;
}else{
    echo 0;
}






